//
//  XYLayoutManager.m
//  NemoSDKDemo
//
//  Created by 高超 on 2020/7/31.
//  Copyright © 2020 JackYang. All rights reserved.
//

#import "XYLayoutManager.h"
#import "NemoCustomLayoutElement+NemoReference.h"

@interface XYLayoutManager ()

@end

@implementation XYLayoutManager

+ (XYLayoutManager *)sharedInstance {
    static XYLayoutManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[XYLayoutManager alloc] init];
    });
    return manager;
}

- (void)customLayout:(NemoRosters *)meetingInfo indexPage:(NSInteger)indexPage {
    if (indexPage > 0) {
        //对称布局请流
        [self _customLayoutSymmetry:meetingInfo indexPage:indexPage];
    }else {
        //非对称布局请流
        [self _customLayoutAsymmetry:meetingInfo indexPage:indexPage];
    }
}

- (void)_customLayoutSymmetry:(NemoRosters *)meetingInfo indexPage:(NSInteger)indexPage {
    
    NemoCustomLayoutInfo *layoutInfo = [[NemoCustomLayoutInfo alloc] init];
    
    if (indexPage == 1) {
        //对称第1页，请5路
        for (int idx=0; idx<meetingInfo.peopleRosters.count; idx++) {
            NemoRosterInfo *roster = [meetingInfo.peopleRosters objectAtIndex:idx];
            if (meetingInfo.peopleRosters.count <= 4) {
                //小于等于4个窗口，请多个360p
                [layoutInfo.layoutElements addObject:[NemoCustomLayoutElement layoutSymmetricMultipleStream:roster.participantId]];
            }else {
                //大于4个窗口，全部请180p
                [layoutInfo.layoutElements addObject:[NemoCustomLayoutElement layoutPeopleThumbStream:roster.participantId]];
            }
            if (layoutInfo.layoutElements.count == 5) {
                break;
            }
        }
        [[XYSDKManager sharedInstance] setLayoutPolicy:layoutInfo];
    }else {
        //对称第2页开始，请6路，均为180p
        for (NSInteger idx=(indexPage-1)*6-1; idx<meetingInfo.peopleRosters.count; idx++) {
            NemoRosterInfo *roster = [meetingInfo.peopleRosters objectAtIndex:idx];
            [layoutInfo.layoutElements addObject:[NemoCustomLayoutElement layoutPeopleThumbStream:roster.participantId]];
            if (layoutInfo.layoutElements.count == 6) {
                break;
            }
        }
        [[XYSDKManager sharedInstance] setLayoutPolicy:layoutInfo];
    }
}

- (void)_customLayoutAsymmetry:(NemoRosters *)meetingInfo indexPage:(NSInteger)indexPage {
    
    NSArray *peopleRosters = [self _sortRoster:meetingInfo];
    meetingInfo.peopleRosters = peopleRosters;
    
    NemoCustomLayoutInfo *layoutInfo = [[NemoCustomLayoutInfo alloc] init];
    
    if (self.whiteboardState == XYWhiteboardState_Start) {
        //白板开启情况下，全屏显示白板
        [[XYSDKManager sharedInstance] setLayoutPolicy:layoutInfo];
        
    }else if (meetingInfo.contentRosters.count > 0) {
        //分享content情况下，全屏显示content，1080p
        NemoRosterInfo *contentRoster = [meetingInfo.contentRosters objectAtIndex:0];
        [layoutInfo.layoutElements addObject:[NemoCustomLayoutElement layoutContentHDStream:contentRoster.participantId]];
        NemoRosterInfo *peopleRoster = [meetingInfo.peopleRosters objectAtIndex:0];
        [layoutInfo.layoutElements addObject:[NemoCustomLayoutElement layoutPeopleThumbStream:peopleRoster.participantId]];
        [[XYSDKManager sharedInstance] setLayoutPolicy:layoutInfo];
        
    }else{
        //people 大窗口720p，小窗口180p
        for (int idx=0; idx<meetingInfo.peopleRosters.count; idx++) {
            NemoRosterInfo *roster = [meetingInfo.peopleRosters objectAtIndex:idx];
            if (idx == 0) {
                [layoutInfo.layoutElements addObject:[NemoCustomLayoutElement layoutPeopleHDStream:roster.participantId]];
            }else {
                [layoutInfo.layoutElements addObject:[NemoCustomLayoutElement layoutPeopleThumbStream:roster.participantId]];
            }
            if (layoutInfo.layoutElements.count == 6) {
                break;
            }
        }
        [[XYSDKManager sharedInstance] setLayoutPolicy:layoutInfo];
    }
}


- (NSArray *)_sortRoster:(NemoRosters *)rosters {
    //排序
    NSArray *outputRosters = [rosters.peopleRosters sortedArrayUsingComparator:^NSComparisonResult(NemoRosterInfo *r1, NemoRosterInfo *r2) {
        if (r1.participantId == rosters.activeSpeakerPid &&
            r2.participantId != rosters.activeSpeakerPid) {
            return NSOrderedAscending;
        }else if (r1.participantId != rosters.activeSpeakerPid &&
                  r2.participantId == rosters.activeSpeakerPid) {
            return NSOrderedDescending;
        }
        
        return NSOrderedSame;
    }];
    return outputRosters;
}

@end
